<?php
class AdminAction extends PublicAction{
		
	//缩略图地址
	protected function litimg($path){
		$pic_server=C('TMPL_PARSE_STRING.__PICSERVER1__');
		$pic_upload=C('TMPL_PARSE_STRING.__UPLOAD__');
		//拆分缩略图路径
		if(!empty($path)){
			$paths=explode('/',$path);
			$j=count($paths);
			if($paths[0]=='cj'){
				//如果是转换过来的图片则直接返回图片
				return $pic_server.$pic_upload.'/'.$path;
			}elseif($j===2){
				//如果非采集图片则在图片名前加s_
				$paths=explode('/',$path);
				$paths[1]='s_'.$paths[1];
				$paths=implode('/',$paths);
				return $pic_server.$pic_upload.'/'.$paths;
			}else{
				//内容中提以的图片
				$paths[$j-1]='s_'.$paths[$j-1];
				$paths=implode('/',$paths);
				$paths=$pic_server.ltrim($paths,'.');
				return $paths;
			}
		}else{
			return __PUBLIC__.'/admin/images/upload-pic.png';
		}
	}

	//导航栏目排序 参数：模型ID
	protected function typelist($data){
		$menu=M('Menu');
		//判断是获取所有模型还是某个模型的栏目
		if(empty($data)){
			$list=$menu->field('typeid,name,seotitle,catid,pid,path,keywords,description typenum,url,concat(path,",",typeid) as bpath')->order('bpath')->select();
		}else{
			$list=$menu->where(array('catid'=>$data))->field('typeid,name,seotitle,catid,pid,path,keywords,description typenum,url,concat(path,",",typeid) as bpath')->order('bpath')->select();
		}			
			  					
		return $list;
		
	}

	protected function typeselect($data){
		
		$typelist=$this->typelist($data);

		$f='——';
		$j=count($typelist);
		$x=null;
		for($i=0;$i<($j);$i++){
			$s=count(explode(',',$typelist[$i]['path']));
			for($n=0;$n<($s-1);$n++){
				{$x.=$f;}
			}
			$type[$typelist[$i]['typeid']]=$x.$typelist[$i]['name'];
			if(isset($x)){unset($x);}
		}

		return $type;
	}


	//内容添加、编辑基本数据整理
	protected function _data($data,$table1,$table2){

		$data['addtime']=strtotime($data['addtime']);		//添加时间
		
		if($data['stick']){
			for($i=0,$j=count($data['stick']);$i<$j;$i++){		//自定义状态值
				$stick[$i]=$data['stick'][$i];
			}
			$data['stick']=implode(',',$stick);			
		}	
		if($dta['allow_comment']){
			$data['allow_comment']=implode(',',$data['allow_comment']);
		}

		if($table1){
			$data[$table1]=array(
				'keywords'=>$data['keywords'],
				'description'=>$data['description'],
				'tag'=>$data['tag'],
				'sourcename'=>$data['sourcename'],
				'sourceurl'=>$data['sourceurl'],
				'userstep'=>$data['userstep'],
				'stick'=>$data['stick'],
				'filename'=>$data['filename'],
				'url'=>$data['url'],
			);		
			unset($data['keywords'],$data['sourceurl'],$data['description'],$data['tag'],$data['sourcename'],$data['url'],$data['userstep'],$data['stick'],$data['filename'],$data['url']);	
		}

		//组图
		if($table2=='picurl'){
			$res=array();
			for($i=0,$j=count($data['pics']);$i<$j;$i++){
				if($data['pid'][$i]){
					$res[$i]['pid']=$data['pid'][$i];
				}
				$res[$i]['pics']=$data['pics'][$i];
				$res[$i]['picname']=$data['picname'][$i]=='请填写图片名称'?Null:$data['picname'][$i];
				$res[$i]['piccontent']=$data['piccontent'][$i]=='请填写图片描述'?Null:$data['piccontent'][$i];
			}

			$data[$table2]=$res;
			unset($data['pics'],$data['picname'],$data['piccontent'],$data['pid']);
		}
			
		return $data;
		//返回数据：主表为一维数据，副表为二维数据
	}






	static protected function pic_uploads($data){
		//dump($data);
		$attachment=M('Attachment');
		$pic_path=$attachment->getfield('id,path');	//缩略图保存目录

		$pic_upload=C('TMPL_PARSE_STRING.__UPLOAD__');
		$pic_server=C('TMPL_PARSE_STRING.__PICSERVER1__');
		$list=C('LIST');
		$domain='http://'.$_SERVER['SERVER_NAME'];
        
		//编辑器中的图片地址
		$data['content']=stripslashes($data['content']);
		$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/"; 
		preg_match_all($pattern,$data['content'],$content_pic); 

		//提取内容中的图片地址
		foreach($content_pic[1] as $val){
			$val=ltrim($val,$domain);
			$val=ltrim($val,$pic_server);
			$val=ltrim($val,$list);
			$val=ltrim($val,$pic_upload);
			$val=ltrim($val,'/');
			$pic_list[]=$pic_upload.'/'.$val;
		}

		//提取组图地址
		if(!empty($data['pics'])){
			foreach($data['pics'] as $val){
				$val=$pic_path[4].$val;
				$val=ltrim($val,$domain);
				$val=ltrim($val,$pic_server);
				$val=ltrim($val,$list);
				$val=ltrim($val,$pic_upload);
				$val=ltrim($val,'/');
				$pic_list[]=$pic_upload.'/'.$val;
			}		        			
		}

		//提取缩略图地址
		if(!empty($data['pic'])){
			$val=$data['pic'];
			$val=ltrim($val,$domain);
			$val=ltrim($val,$pic_server);
			$val=ltrim($val,$list);
			$val=ltrim($val,$pic_upload);
			$val=ltrim($val,'/');
			$pic_list[]=$pic_upload.'/'.$val;
		}

		$list=array();
		for($i=0,$j=count($pic_list);$i<$j;$i++){
			$list[$i]['path']=$pic_list[$i];
			$list[$i]['date']=time();
		}


		$j=count($list);
		for($i=0;$i<$j;$i++){
			if(!empty($data['id'])){
				$w['articleid']=$data['id'];
				$w['path']=$list[$i]['path'];
				$result=M('Files')->where($w)->field('id')->find();
			}else{
				$w['tid']=$data['typeid'];
				$w['path']=$list[$i]['path'];
				$result=M('Files')->where($w)->field('id')->find();
			}	

			$list[$i]['id']=$result['id'];	
		}


		return $list;

	}




}
